<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=sWMQitwma2xiwXYmz02GyKt2e2U2VEpv"></script>
    <script src="https://code.bdstatic.com/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <title>单个逆地址解析</title>
    <style>
        body,html{
           width: 100%;
           height: 100%;
           padding: 0;
           margin: 0;
       }
       #container{
           width: 100%;
           height: 800px;
           overflow: hidden;
       }
       #result{
           position: fixed;
           top: 10px;
           left: 10px;
           padding: 0px 7px;
           min-width: 350px;
           height: 70px;
           line-height: 35px;
           background: #fff;
           box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
           border-radius: 7px;
           z-index: 99;
       }
       #btn{
        width: 110px;
        height: 40px;
        margin-left: 850px;
       }
       #btn1{
        position: relative;
        width: 110px;
        height: 40px;
        top: 0px;
        left: 0px;
       }
       .address{
        position: relative;
        width: 200px;
        height: 30px;
        top: 0px;
        left: 850px;
       }
    </style>
</head>
<body>
    <div id='container'></div>
    <div id='result'>
        点击展示详细的地址：
        <div id='result_l'></div>
    </div>
    <p> <input type='text' class="address" placeholder="请输入详细地址（街道、门牌等）"/>
        <button id="btn">添加为收货地址</button>
        <button id="btn1">返回支付页面</button></p>
    <script src="../js/jquery-3.7.1.js"></script>
    <script>
        //实例化地图
        let province = '';
        let city = '';
        let area = '';
        let street = '';
        
        var map = new BMapGL.Map('container');
        map.centerAndZoom(new BMapGL.Point(112.504184,26.914485), 15);
        map.enableScrollWheelZoom(); // 启用滚轮放大缩小
        var geoc = new BMapGL.Geocoder();
        map.addEventListener('click', function(e){
            //清除地图上所有的覆盖物
            map.clearOverlays();
            console.log(e);
            var pt = e.latlng;
            var marker = new BMapGL.Marker(new BMapGL.Point(pt.lng, pt.lat));
            map.addOverlay(marker);
            geoc.getLocation(pt, function(rs){
                var addComp = rs.addressComponents;
                $('#result_l').text(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
                province = addComp.province;
                city = addComp.city;
                area = addComp.district;
                street = addComp.street + addComp.streetNumber;
            })
        })
        $("#btn").click(() => {
            let address = $(".address").val();
            let addr = address+street;
            console.log(addr);
            if(province == '' || address == ''){
                alert("请选中你的地址");
                return;
            }else{
                $.ajax({
				type:"POST",//请求方式
				url:"../addrController/apiAddr",//请求地址
				data:{//请求参数
					province:province,
                    city:city,
                    area:area,
                    address:addr
				},
				async:true,//同异步	true异步
				dataType:"text",//返回的数据格式默认text
				success(data){//请求成功的回调函数
					if(data>0){
						alert("添加地址成功");
					}else{
						alert("添加地址失败");
					}
				},
				error(err){//请求失败的回调函数
					console.log(err);
				}
			});
            } 
        });
        $("#btn1").click(() => {
            //跳转到支付页面
            window.location.href = "checkout.html";
        });
    </script>
</body>